// The Vue build version to load with the `import` command
// (runtime-only or standalone) has been set in webpack.base.conf with an alias.
import Vue from 'vue'
import App from './App'
import router from './router'
import axios from 'axios'
import QS from 'qs'
import store from './store'
import mandMobile from 'mand-mobile'
import 'mand-mobile/lib/mand-mobile.css'
import "reset.css";
import VueClipboard from 'vue-clipboard2'
import preview from 'vue-photo-preview'
import 'vue-photo-preview/dist/skin.css'

//刷新保存状态
if (sessionStorage.getItem("store")) {
  store.replaceState(
    Object.assign(
      {},
      store.state,
      JSON.parse(sessionStorage.getItem("store"))
    )
  );
  // sessionStorage.removeItem("store")
}

//监听，在页面刷新时将vuex里的信息保存到sessionStorage里
window.addEventListener("beforeunload", () => {
  sessionStorage.setItem("store", JSON.stringify(store.state));
});

Vue.prototype.$axios = axios;
Vue.prototype.qs = QS;
Vue.use(mandMobile);
Vue.config.productionTip = false
Vue.use(VueClipboard)
let options = {
  fullscreenEl: false,
  history: true
};
Vue.use(preview, options)


router.beforeEach((to, from, next) => {
  /* 路由发生变化修改页面title */
  if (to.meta.title) {
    document.title = to.meta.title
  }
  next()
  if (to.matched.some(record => record.meta.requireAuth)) {  // 判断该路由是否需要登录权限
    console.log(store.state.userInfo.mobile, store.state.userInfo.id)
    if (store.state.userInfo.mobile == undefined && store.state.userInfo.id == undefined || store.state.userInfo.mobile == "") {
      console.log('未登录')
      var reg = new RegExp("(^|&)" + 'code' + "=([^&]*)(&|$)");
      var r = window.location.search.substr(1).match(reg);
      var code = null;
      if (r != null){
        var code = unescape(r[2]);
      }
      var local = encodeURIComponent("https://xm.sqyzdh.com");
      var APPID = "wx8d06ea96180136be";
      if (code == null || code === "" || code == store.getters.getCode) {
        window.location.href =
          "https://open.weixin.qq.com/connect/oauth2/authorize?appid=" +
          APPID +
          "&redirect_uri=" +
          local +
          "&response_type=code&scope=snsapi_userinfo&state=#wechat_redirect";
      } else {
        store.commit("saveCode", code);
        let params = {
          code
        };
        https
          .fetchGet(config.getLoginStatus, params)
          .then(res => {
            if (res.data.code == 1) {
              let userInfo = res.data.data;
              store.commit("saveUserInfo", userInfo);
            } else if (res.data.code == 0) {
              let wxInfo = res.data.data;
              store.commit("saveWxInfo", wxInfo);
              router.push({
                path: "/register"
              });
            } else {
              Toast.failed(res.data.msg);
            }
          })
          .catch(err => {
            console.log(err);
          });
      }
    } else {
      console.log('已经登录',to.fullPath)
      next();
    }
  } else {
    console.log('不需要登录')
    next();
  }
});

/* eslint-disable no-new */
new Vue({
  el: '#app',
  router,
  store,
  components: { App },
  template: '<App/>'
})
